home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / smail-3.1.28 / util / getmap.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  1992-07-11  |  6.1 KB  |  233 lines

  1. #! /bin/sh
  2. # @(#)util/getmap.sh    1.6 7/11/92 11:39:55
  3. #
  4. #    Copyright (C) 1988 Ronald S. Karr and Landon Curt Noll
  5. #    Copyright (C) 1992 Ronald S. Karr
  6. #
  7. # See the file COPYING, distributed with smail, for restriction
  8. # and warranty information.
  9.  
  10. # getmap - unshar usenet map articles into the UNSHAR_MAP_DIR directory
  11. #
  12. # usage: getmap [-m mapdir] [-w workdir] [-b batchfile] [-u username]
  13. #
  14. #     -m mapdir    - 'mapdir' is where maps are unpacked,
  15. #              by default 'mapdir' is $UNSHAR_MAP_DIR
  16. #     -w workdir    - where logs and default batch files are kept,
  17. #              by default 'workdir' is 'mapdir'/work
  18. #      -b batch    - 'batch' is a file of filenames to unshar,
  19. #              '-' impiles read filenames from stdin,
  20. #              by default 'batch' is 'workdir'/batch
  21. #     -u username    - errors are Emailed to 'username' rather than
  22. #              Posmaster, the username '-' imples that
  23. #              the errors should be written to standard error
  24. #     -n newsgroups    - allowed newsgroups for articles
  25. #
  26. # unsharmap errors will be emailed to the postmaster.
  27. #
  28. # default files:
  29. #    $UNSHAR_MAP_DIR            - maps are unpacked here
  30. #    $UNSHAR_MAP_DIR/work/getmap.log    - log of getmap activity and errors
  31. #    $UNSHAR_MAP_DIR/work/getmap.err    - like getmap.log + skipped lines
  32. #                      removed if no major unshar errors
  33. #    $UNSHAR_MAP_DIR/work/batch        - default list of artciles to unshar
  34. #    batch.work                - tmp copy of batch work, normally
  35. #                      $UNSHAR_MAP_DIR/work/batch.work,
  36. #                      ignored if "-w -"
  37.  
  38. # locations and constants
  39. #
  40. PATH="X_UTIL_PATH_X:X_SECURE_PATH_X"; export PATH
  41. UTIL_BIN="X_UTIL_BIN_DIR_X"
  42. UNSHAR_MAP_DIR="X_UNSHAR_MAP_DIR_X"
  43. NEWSSPOOL="X_NEWS_SPOOL_DIR_X"
  44. UNSHAR=$UTIL_BIN/unsharmap
  45. GETOPT=$UTIL_BIN/getopt
  46. AWKFILE=$UTIL_BIN/getmap.awk
  47. GLEEM=$UTIL_BIN/gleem
  48.  
  49. # set defaults value for location that can change by option
  50. #
  51. MAPDIR=$UNSHAR_MAP_DIR
  52. WORKDIR=$MAPDIR/work
  53. BATCH=$WORKDIR/batch
  54. USERNAME=Postmaster
  55. NEWSGROUPS=comp.mail.maps
  56.  
  57. # parse args
  58. #
  59. PROG=$0
  60. USAGE="usage: $PROG [-m mapdir] [-w workdir] [-b batch] [-u username]"
  61. set -- `$GETOPT -n $PROG -q m:w:b:u:n:s: $*`
  62. if [ $? != 0 ]; then
  63.     echo $USAGE 1>&2
  64.     exit 1
  65. fi
  66. for i in $*; do
  67.     case $i in
  68.     -m) MAPDIR=$2; shift 2;;
  69.     -w) WORKDIR=$2; shift 2;;
  70.     -b) BATCH=$2; shift 2;;
  71.     -u) USERNAME=$2; shift 2;;
  72.     -n) NEWSGROUPS=$2; shift 2;;
  73.     -s) NEWSSPOOL=$2; shift 2;;
  74.     --) shift; break;;
  75.     esac
  76. done
  77. if [ "$#" -ne 0 ]; then
  78.     echo $USAGE 1>&2
  79.     exit 2
  80. fi
  81. if [ "$BATCH" = "-" ]; then    # catch stdin case
  82.     BATCH=
  83. fi
  84. if [ "$USERNAME" = "-" ]; then    # catch the cat errors to stderr case
  85.     REPORT="cat 1>&2"
  86. else
  87.     REPORT="X_SMAIL_NAME_X -ep -i -t"
  88. fi
  89.  
  90. # set locations now that we have the flags
  91. #
  92. LOG=$WORKDIR/getmap.log
  93. ERROR_LOG=$WORKDIR/getmap.err
  94. REBUILD=$WORKDIR/getmap.rebuild
  95.  
  96. # be sure the working file does not exist, unless we read from stdin
  97. #
  98. if [ ! -z "$BATCH" ]; then
  99.     BATCH_TMP="$BATCH".work
  100.     if [ -f "$BATCH_TMP" ]; then
  101.         echo "$PROG: working batch file $BATCH_TMP exists" 1>&2
  102.         echo "$PROG: remove $BATCH_TMP by hand if stale" 1>&2
  103.         exit 3
  104.     fi
  105. fi
  106.  
  107. # setup log files
  108. #
  109. if [ ! -z "$BATCH" ]; then
  110.     BATCH_MSG="$PROG: starting work on $BATCH at `date`"
  111. else
  112.     BATCH_MSG="$PROG: starting work on [stdin] at `date`"
  113. fi
  114. echo "$BATCH_MSG" > $ERROR_LOG
  115. if [ "$?" -ne 0 ]; then
  116.     echo "$PROG: can not clear $ERROR_LOG" 1>&2
  117.     exit 4
  118. fi
  119. touch $LOG 2>/dev/null
  120. if [ "$?" -ne 0 ]; then
  121.     echo "$PROG: can not write to $LOG" 1>&2
  122.     echo "$PROG: can not write to $LOG" >> $ERROR_LOG
  123.     (if [ "$USERNAME" != "-" ]; then
  124.     echo "To: $USERNAME"
  125.     echo "Subject: getmap error"
  126.     echo ""
  127.     echo ""
  128.      fi
  129.      echo "getmap error log follows -----"
  130.      cat $ERROR_LOG
  131.      echo "end of getmap error log =====") | eval "$REPORT"
  132.     exit 5
  133. fi
  134.  
  135.  
  136. # save the batch file, if not from stdin
  137. #
  138. if [ ! -z "$BATCH" ]; then
  139.     # do nothing if no batch file or no work in the batch file
  140.     if [ ! -f "$BATCH" -o ! -s "$BATCH" ]; then
  141.     echo "$PROG: no work in $BATCH" >> $LOG
  142.     rm -f $ERROR_LOG
  143.         exit 0
  144.     fi
  145.     mv $BATCH $BATCH_TMP
  146.     if [ "$?" -ne 0 ]; then
  147.         echo "$PROG: could not move $BATCH to $BATCH_TMP" 1>&2
  148.         echo "$PROG: could not move $BATCH to $BATCH_TMP" >> $ERROR_LOG
  149.     (if [ "$USERNAME" != "-" ]; then
  150.         echo "To: $USERNAME"
  151.         echo "Subject: getmap error"
  152.         echo ""
  153.         echo ""
  154.      fi
  155.      echo "getmap error log follows -----"
  156.      cat $ERROR_LOG
  157.      echo "end of getmap error log =====") | eval "$REPORT"
  158.     cat $ERROR_LOG >> $LOG
  159.     exit 6
  160.     fi
  161.  
  162. # if work from stdin, prep a file to be used to save a copy
  163. #
  164. else
  165.     BATCH_TMP=$WORKDIR/getmap.in$$
  166.     cat /dev/null > $BATCH_TMP
  167.     if [ "$?" -ne 0 ]; then
  168.         echo "$PROG: could not clear $BATCH_TMP to hold a copy of [stdin]" 1>&2
  169.         echo "$PROG: could not clear $BATCH_TMP to hold a copy of [stdin]" >> $ERROR_LOG
  170.     (if [ "$USERNAME" != "-" ]; then
  171.         echo "To: $USERNAME"
  172.         echo "Subject: getmap error"
  173.         echo ""
  174.         echo ""
  175.      fi
  176.      echo "getmap error log follows -----"
  177.      cat $ERROR_LOG
  178.      echo "end of getmap error log =====") | eval "$REPORT"
  179.     cat $ERROR_LOG >> $LOG
  180.     exit 7
  181.     fi
  182. fi
  183.  
  184. # process the map artcile files
  185. #
  186. if [ ! -z "$BATCH" ]; then
  187.     $UNSHAR -d $MAPDIR -n $NEWSGROUPS -s $NEWSSPOOL < $BATCH_TMP >> $ERROR_LOG
  188.     STATUS=$?
  189. else
  190.     tee -a $BATCH_TMP | $UNSHAR -d $MAPDIR >> $ERROR_LOG
  191.     STATUS=$?
  192. fi
  193.  
  194. # note if we unpacked anything
  195. if [ -s "$BATCH_TMP" ]; then
  196.     touch $REBUILD
  197. fi
  198.  
  199. # log the activity
  200. #
  201. cat $ERROR_LOG >> $LOG
  202.  
  203. # post processing - look for errors to report
  204. #
  205. if [ "$STATUS" -ne 0 ]; then
  206.     # form the mail message header
  207.     (if [ "$USERNAME" != "-" ]; then
  208.     echo "To: $USERNAME"
  209.     echo "Subject: getmap error"
  210.     echo ""
  211.     echo ""
  212.      fi
  213.      echo "getmap error log follows -----"
  214.      cat $ERROR_LOG
  215.      echo "end of getmap error log ====="
  216.      echo ""
  217.      echo ""
  218.      if [ ! -z "$BATCH" ]; then
  219.          echo "$BATCH_TMP work queue follows -----"
  220.          cat $BATCH_TMP
  221.          echo "end of $BATCH_TMP work queue ====="
  222.      else
  223.          echo "[stdin] work queue follows -----"
  224.      cat $BATCH_TMP
  225.          echo "end of [stdin] work queue ====="
  226.      fi) | eval "$REPORT"
  227. else
  228.     rm -f $ERROR_LOG    # no error, so remove the error log
  229. fi
  230. rm -f $BATCH_TMP
  231.  
  232. exit 0
  233.